﻿using System.Configuration;
using System.Data;
using System.Windows;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using TxtManager.Infrastructure.Context;
using TxtManager.WPF.ViewModels;

namespace TxtManager.WPF
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : System.Windows.Application
    {

        public static IServiceProvider ServiceProvider { get; private set; } = null!;


        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            // 初始化DI容器
            ServiceProvider = WPF.Startup.ConfigureServices();

            // 执行数据库迁移
            using var scope = ServiceProvider.CreateScope();
            var dbContext = scope.ServiceProvider.GetRequiredService<TextDbContext>();
            dbContext.Database.Migrate();

            // 创建主窗口
            var mainWindow = new MainWindow();
            mainWindow.DataContext = ServiceProvider.GetRequiredService<MainWindowViewModel>();
            mainWindow.Show();
        }




    }

}
